import grails.converters.JSON
import org.hibernate.FetchMode as FM
class ApiController {
    def publishService
    def classerService

    def index = { 
        def postBody = [title:'成绩单',content:'生生世世生生世世生生世世是',authorID:'7',studentId:'',schoolId:'1',classesHostId:'11']
        publishService.createPost(postBody)
    }

    def createPost = {
        
        publishService.createPost(params)
    }

        
    def upClasser = {    
        log.info("王鹤来测试")
        
        return classerService.upClasserName(params.hostId,URLDecoder.decode(params.name, "UTF-8"))
    }

    def getCommentsList = {
        def classId = params.classId;
        if(classId){
            log.info(Comments.findAllByClassId(classId.toLong()).size())
            render Comments.findAllByClassId(classId.toLong(),[sort:"createTime", order:"desc"]) as JSON
        }else{
            render (['message' : '没有家长的问题！'] as JSON)
        }        
    }

    def getCommentsAndRemark = {
        def commentId = params.commentId;
        if(commentId){
            log.info(commentId.toLong())
            def commentsInstance = Comments.get(commentId.toLong())
            render (['success': true, 'commentsInstance': commentsInstance, 'remarks': Remark.findAllByComments(commentsInstance,[sort:"createTime", order:"desc"])] as JSON)
        }else{
            render (['message' : '没有家长的问题！'] as JSON)
        }       
    }

    def saveRemark = {
            def remarkInstance = new Remark(params)
            
            
            log.info(params.commentId)
            log.info(params.content)
            log.info(params.classId)
            def comments = Comments.get(params.commentId.toLong())
            remarkInstance.comments = comments 
            
            if (remarkInstance.save(flush: true)) {
                flash.message = "发送成功"
                render (['message' : '保存成功','success': true] as JSON)
            }
            else {
                render (['message' : '保存失败','success': false] as JSON)
            }
    }
}
